<template>
  <div class="edit-department">
    <a-modal
      width="800px"
      :title="updateTitles"
      :visible="visible"
      :confirm-loading="confirmLoading"
      :maskClosable="false"
      @ok="handleSubmit"
      @cancel="handleCancel"
    >
      <a-spin :spinning="spinning" tip="正在加载数据中...">
        <a-form :form="form">
          <a-row :gutter="10">
            <!-- fix: 修改时新闻名称不能修改 -->
            <a-col :span="24">
              <a-form-item label="问题提问" :labelCol="{ span: 4 }" :wrapperCol="{ span: 20 }">
                <a-input
                  v-decorator="[
                    'NAME',
                    {
                      rules: [{ required: true, message: '', whitespace: true, trigger: 'change' }],
                    },
                  ]"
                  autocomplete="off"
                  placeholder="请输入问题提问"
                />
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="问题回复" :labelCol="{ span: 4 }" :wrapperCol="{ span: 20 }">
                <a-textarea 
                  v-decorator="[
                    'MS',
                    {
                      rules: [{ required: true, message: '', whitespace: true, trigger: 'change' }],
                    },
                  ]"
                  autocomplete="off"
                  placeholder="请输入问题回复"
                />
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
import pick from 'lodash.pick'
import { setDepartments, getDepartmentSelect } from '@/api/system'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { getFWLBDrptApi, postAddNewsApi, getCsglListApi, postupdateNewsApi } from '@/api/service'
import { validFixedTelphone, validTelphone } from '@/utils/validate'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    propsData: {
      type: Object,
      default: () => {
        return {}
      },
    },
  },
  data() {
    return {
      confirmLoading: false,
      treeName:'',
      headers: {
        Authorization: storage.get(ACCESS_TOKEN),
        GF: this.$store.getters.userid,
      },
      spinning: false,
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      parentTreeList: [
        {
          NAME: '无',
          ID: 0,
          children: [],
        },
      ],
      levelList: [],
      hoturl:'',
      iconurl:'',
      FWLBTBparameter:[],
      form: this.$form.createForm(this, { name: 'department' }),
    }
  },
  watch: {
    propsData: function () {
      this.fetchDepartment()
    },
  },
  computed: {
    // levelList:treeNameLookup(),
    updateTitles: function () {
      const {
        propsData: { type },
      } = this
      if (type === 'add') {
        return '新增新闻'
      } else if (type === 'addChild') {
        return '新增子新闻'
      } else {
        return '修改新闻'
      }
    },
  },
  methods: {
    fetchDepartment() {
      this.spinning = true
      const {
        propsData: { type, record },
        form: { setFieldsValue },
      } = this
      const tree = new Promise((resolve, reject) => {
        getFWLBDrptApi({
          type: 'ddepartemnt',
          Contype: this.$store.getters.loginName,
        }).then((res) => {
          resolve(res.data)
        })
      })
      const category = new Promise((resolve, reject) => {
        getFWLBDrptApi().then((res) => {
          resolve(res.data)
        })
      })
      getCsglListApi({
        ClassKey: 'FWLBTB',
      }).then((res) => {
        this.FWLBTBparameter = res.data
      })
      Promise.all([tree, category])
        .then((res) => {
          this.parentTreeList[0].children = res[0]
          console.log(this.parentTreeList)
          this.levelList = res[1]
          if (type === 'addChild') {
            setFieldsValue({ Fid: record.ID })
          }
          if (type === 'add' || type === 'addChild') return
          record.BBDJ = record.BBDJ ? Number(record.BBDJ) : undefined
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['Fid', 'NAME','MS', 'BBDJ', 'BBDZ', 'BBDH', 'DepartmentHead', 'FZRDH']))
          })
        })
        .catch(() => {
          this.$message.error('网络请求失败, 请稍后重试')
        })
        .finally(() => {
          this.spinning = false
        })
    },
    handleSubmit() {
      const {
        form: { validateFields },
        propsData: { type, record },
        $message,
      } = this
      validateFields((err, values) => {
        console.log('values',values);
        if (!err) {
          this.confirmLoading = true
          const submitData = Object.assign({}, values, {})
          if (type === 'update') {
            submitData.ID = record.ID
            postupdateNewsApi(Object.assign({}, values, {
            ID:record.ID,
            Fid:1,
          }))
            .then((res) => {
              if (res.code === 200) {
                $message.success(res.message || '提交成功', 3)
                this.handleCancel()
                this.$parent.$parent.$refs.table.refresh(true)
              } else {
                $message.error(res.message || '提交失败', 3)
              }
            })
            .catch((err) => {
              $message.error(err, 3)
            })
            .finally(() => {
              this.confirmLoading = false
            })
          }else{
            postAddNewsApi(Object.assign({}, values, {
            Fid:1,
          }))
            .then((res) => {
              if (res.code === 200) {
                $message.success(res.message || '提交成功', 3)
                this.handleCancel()
                this.$parent.$parent.$refs.table.refresh(true)
              } else {
                $message.error(res.message || '提交失败', 3)
              }
            })
            .catch((err) => {
              $message.error(err, 3)
            })
            .finally(() => {
              this.confirmLoading = false
            })
          }

        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.$emit('update:visible', false)
    },
  },
}
</script>
